﻿if (typeof ($) != "undefined") {
    $(document).ready(function () {
        GetProductDataByOrderId($("#OrderID").val());
    });
}

//修改查看时加载数据,obj-行对象
var fnCustomeGetDataSourceEvent = function (obj, i) {
    fnCreateItemTypeSelect(obj, i);
}

//分类下拉框,obj-行对象
var fnCreateItemTypeSelect = function (obj, i) {
    var ItemCateoryIDObj = document.getElementsByName("OrderProductId")[i];
    var AllStr = ""; debugger
    $(ItemCateoryIDObj).empty();
    var url = "../Extend/AjaxData/JsonList.aspx?r=" + Math.random() + "&entity=V_CrmCustomerInfoOrderProduct&condition=ID='" + $("#OrderID").val() + "'";
    $.get(url, function (d, s) {
        var list = eval("(" + d + ")");
        for (var j = 0; j < list.rows.length; j++) {
            AllStr = AllStr + ("<option value='" + list.rows[j].OrderProductId + "'>" + list.rows[j].ProductName + "</option>");
        }
        if (AllStr != "") {
            $(ItemCateoryIDObj).append(AllStr);
            jsSelectItemByValue(ItemCateoryIDObj, _DataTable.Rows[i].OrderProductId);
        }
    });
}

//下拉列表改变
var getText = function (obj) {
    fnItemSelect(getTableRow(obj));
}

//下拉框选择事件,obj-行对象
var fnItemSelect = function (obj, i) {
    var ItemID = $(obj).val();
    var ItemName = $(obj).find("option:selected").text()
    var ItemRemarkObj = document.getElementsByName("txtOrderProductId")[i];

    var url = "../Extend/AjaxData/jsonlist.aspx?entity=bpm_tuikuanshenqing&condition=ID='" + ItemID + "'";
    $.get(url, function (d, s) {
        var list = eval("(" + d + ")");
        for (var o in list.rows) {
            //debugger;
            var t = list.rows[o].Remark;
            if (typeof (t) != "undefined") {
                $(ItemRemarkObj).val(t);
            }
        }
    });
}

//自定义行数据
var fnCustomeRowAddEvent = function (obj) {
    //obj.find("name=OrderProductId").append('<option value="5">Option5</option>'); 
    if (lists.rows != null && lists.rows != undefined && lists.rows.length > 0) {
        for (var i = 0; i < lists.rows.length; i++) {
            $("#OrderProductId", obj).append('<option value="' + lists.rows[i].OrderProductId + '">' + lists.rows[i].ProductName + '</option>');
        }
        //默认选中第一行
        $("#txtOrderProductId", obj).val(lists.rows[0].ProductName);
    } else {
        var tr = $("#Details tbody tr").eq(0);
        tr.remove();
        alert("请选择有产品的订单");
        return;
    }
}

var OrderProductId;
var ValueChanged = function (rowobj) {
    OrderProductId = $("#OrderProductId").val();
    var vOrderId = $("#OrderID").val();
    GetProductDataByOrderId(vOrderId);
}
var TextChanged = function (rowobj) {
}
var lists;
var tempTitle;
//获取产品信息
var GetProductDataByOrderId = function (orderId) {
    //debugger
    var vCondition = "ID = '" + orderId + "' and ProductResignMoney>0"
    var url = "../Extend/AjaxData/JsonList.aspx?r=" + Math.random() + "&entity=V_CrmCustomerInfoOrderProduct&condition=" + vCondition;
    $.get(url, function (d, s) {
        $("#DetailDiv tbody").html("");
        lists = eval("(" + d + ")");
        var hk = 0;

        for (var i = 0; i < lists.rows.length; i++) {
            try {
                hk = hk + parseInt(lists.rows[i].ProductResignMoney);
            } catch (e) {
                hk = 0;
            }
            $("#tuikuanqiyemingchen").val(lists.rows[i].CustomerName);
            $("#DetailDiv tbody").append("<tr>" +
                "<td style='text-align: center'>" + lists.rows[i].ProductName + "</td>" +
                "<td style='text-align: center'>" + lists.rows[i].ProductTypeName + "</td>" +
                "<td style='text-align: center'>" + lists.rows[i].Amount + "</td>" +
                "<td style='text-align: center'>" + lists.rows[i].AccountReceivable + "</td>" +
                "<td style='text-align: center'>" + lists.rows[i].ProductResignMoney + "</td>" +
                "<td style='text-align: center'>" + lists.rows[i].DeliverPersonCount + "</td>" +
                "<td style='text-align: center'>" + lists.rows[i].InvoiceMoney + "</td>" +
                    "</tr>");
        }
        if (lists != undefined && lists.rows.length > 0) {
            //$("#tuikuanqiyemingchen").val(lists.rows[0].CustomerName);
            var vCustomer = $("#tuikuanqiyemingchen").val();
            var vTitle = $("#bpmTitle").val();

            if (vTitle.indexOf('--') < 0) {
                tempTitle = vTitle;
            }

            $("#bpmTitle").val(vCustomer + "--" + tempTitle);
            $("#huikuanjine").val(hk);
        }
    });
}

///设置select中Value="paraValue"的第一个Item为选中  
function jsSelectItemByValue(objSelect, objItemValue) {
    //判断是否存在        
    for (var i = 0; i < objSelect.options.length; i++) {
        if (objSelect.options[i].value == objItemValue) {
            objSelect.options[i].selected = true;
            break;
        }
    }
}

// 1.判断select选项中 是否存在Value="paraValue"的Item        
function jsSelectGetSelectOption(objSelect) {
    var v = "";
    for (var i = 0; i < objSelect.options.length; i++) {
        if (objSelect.options[i].selected == true) {
            v = objSelect.options[i].value;
            break;
        }
    }
    return v;
}

var fnCheckData = function () {
    try {
        var l = document.getElementsByName("ReturnMoney").length
        if (l == 0) {
            return "请新增明细，10秒后提交";
        } else {
            for (var i = 0; i < l; i++) {
                var or = document.getElementsByName("ReturnMoney")[i];
                if (or.value == "") {
                    return "第" + (i + 1) + "行退款明细不能为空，10秒后提交！";
                }
                var or = document.getElementsByName("kaihuhang")[i];
                if (or.value == "") {
                    return "第" + (i + 1) + "行银行开户行不能为空，10秒后提交！";
                }
                var or = document.getElementsByName("kaihuming")[i];
                if (or.value == "") {
                    return "第" + (i+1) + "行银行开户名不能为空，10秒后提交！";
                }
                var or = document.getElementsByName("yinhangzhanghao")[i];
                if (or.value == "") {
                    return "第" + (i + 1) + "行银行账号不能为空，10秒后提交！";
                }
            }
        }
        //return "错误";
    }
    catch (e)
     { return "请新增明细，10秒后提交 或 直接联系联系管理员"; }
}